package com.maxatime.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.maxatime.dao.StudentBasicInfoMapper;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.service.ExcelServce;

@Service
public class ExcelServceImpl implements ExcelServce{
	
	Logger log = LogManager.getLogger();
	
	@Autowired
	StudentBasicInfoMapper studentBasicInfoMapper;
	//数据库导出Excel
	@Override
	public String excelExport() {
		// TODO Auto-generated method stub
		//根据id查询数据
        List<StudentBasicInfo> studentBasicInfo = studentBasicInfoMapper.getPeopleById();
        if (studentBasicInfoMapper == null || studentBasicInfo.isEmpty()) {
            return "failed";
        }
        //生成文件路径
        String newFilePath = "F:\\Eclipse2023";
        //文件名称
        String fileName = "people.xlsx";
        //输出流
        OutputStream out = null;
        //Excel文件
        XSSFWorkbook workBook = new XSSFWorkbook();
        //Excel页脚
        XSSFSheet sheet = workBook.createSheet("数据导出");
        //设置列的宽度
        sheet.setDefaultColumnWidth(16);
        //创建标题行
        XSSFRow titleRow = sheet.createRow(0);
        String[] title = new String[]{"序号", "学号", "姓名", "手机号", "年龄", "证件类型", "证件号码", "详细地址", "邮箱"};
        //设置标题字体样式
        XSSFCellStyle cellStyle = workBook.createCellStyle();
        XSSFFont font = workBook.createFont();
        font.setBold(true);//加粗
        font.setFontHeightInPoints((short) 14);//设置字体大小
        cellStyle.setFont(font);
        //设置标题列
        for (int i = 0; i < title.length; i++) {
            //创建标题的单元格
            XSSFCell titleCell = titleRow.createCell(i);
            //填充标题数值
            titleCell.setCellValue(title[i]);
            //设置样式
            titleCell.setCellStyle(cellStyle);
        }
        //填充数据
        //第一行是标题所以要从第二行开始
        for (int i = 0; i < studentBasicInfo.size(); i++) {
        	StudentBasicInfo studentBasicInfo2 = studentBasicInfo.get(i);
            XSSFRow row = sheet.createRow(i + 1);
            for (int j = 0; j < title.length; j++) {
                XSSFCell titleCell = row.createCell(j);
                String exportKey = title[j];
                switch (exportKey) {
                    case "序号":
                        titleCell.setCellValue(studentBasicInfo2.getStudentId());
                        break;
                    case "学号":
                        titleCell.setCellValue(studentBasicInfo2.getStudentNum());
                        break;
                    case "姓名":
                        titleCell.setCellValue(studentBasicInfo2.getStudentName());
                        break;
                    case "年龄":
                        titleCell.setCellValue(studentBasicInfo2.getMobile());
                        break;
                    case "证件类型":
                        titleCell.setCellValue(studentBasicInfo2.getIdType());
                        break;
                    case "证件号码":
                        titleCell.setCellValue(studentBasicInfo2.getIdNum());
                        break;
                    case "详细地址":
                        titleCell.setCellValue(studentBasicInfo2.getDetailAddr());
                        break;
                    case "邮箱":
                        titleCell.setCellValue(studentBasicInfo2.getEmail());
                        break;
                        
                }
            }
        }
        try {
            File file = new File(newFilePath + File.separator + fileName);
            out = new FileOutputStream(file);
            workBook.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            log.info(e.getMessage());
        }
        return "success";
    }
	

}
